<!DOCTYPE HTML>
<html>
<head>
	<?php
		include("include.php");
		
		function doAction()
		{
			if(!empty($_POST['action']))
			{
				if($_POST['action'] == 'createNewUser')
				{
					//do stuff
					$username = mysql_real_escape_string($_POST['username']);
					$password = mysql_real_escape_string($_POST['password1']);
					
					$success = userClass::addUser($username, $password);
					
					if($success)
						displayConfirmation();
					else
						displayError();
					
				}
			}
			else
				displayNewUserForm();
		
		}
	?>
	
	<script>
		/*$.validator.setDefaults({
			submitHandler: function() { alert("submitted!"); }
		});*/
	
		$().ready(function() {
			$("#createAccountForm").validate({
				rules: {
					username: {
						required: true,
						minlength: 3
					},
					password1: {
						required: true,
						minlength: 5
					},
					password2: {
						required: true,
						minlength: 5,
						equalTo: "#password1"
					}
				},
				messages: {
					username: {
						required: "Please enter a username",
						minlength: "Your username must consist of at least 3 characters"
					},
					password1: {
						required: "Please provide a password",
						minlength: "Your password must be at least 5 characters long"
					},
					password2: {
						required: "Please provide a password",
						minlength: "Your password must be at least 5 characters long",
						equalTo: "Please enter the same password as above"
					}
				}
			});
		});
	</script>
	
	<style>
		#content label{display:inline-block;width:80px;}
		#content label.error{ width:400px; margin-left:5px; display:inline-block; color:red; }
	</style>
					
</head>
<body>
<div class="container">
	<div id="header">
		<?php include("banner.php"); ?>
		
		<div id="login">
			<?php login::displayLoginDiv(); ?>
		</div>
		
		<div class="clear"></div>
		
		<div id="navigation">
			<?php include('navigation.php'); ?>
		</div>
	</div>
	
	<div class="clear"></div>
	
	<div id="body">
		<div id="content">
			<?php 
			
			doAction();
			
			function displayNewUserForm()
			{
			?>
				<h1>New User</h1>
				
				<form id="createAccountForm" name="createAccountForm" action="newuser.php" method="post">
					
					<label for="username">Username:</label>
						<input id="username" type="text" class="text" name="username" />
						<a id="checkAvailability" href="#">Check availability</a><div id="ajaxResponse" style="display:inline-block; margin-left:5px"></div>
						<br/>
					<label for="password1">Password:</label>
						<input id="password1" type="password" class="text" name="password1" />
						<br/>
					<label for="password2" style="float:left;margin:-2px 2px">Re-Type Password:</label>
						<input id="password2" type="password" class="text" name="password2" />
						<br/>
						
					<br/>
					<input type="hidden" name="action" value="createNewUser" />
					<input type="submit" class="submit" value="Create Account">
				</form>
			
			
				<script>
					$("#checkAvailability").click(function (){
						checkAvail();
					});
					
					function checkAvail()
					{
						x = '';
						x = document.forms['createAccountForm']['username'].value;
	
						if( x!='' && x!=null )
						{
							var xmlHttp = getXMLHttp();
						  
							xmlHttp.onreadystatechange = function()
							{
								if(xmlHttp.readyState == 4)
								{
									HandleResponse(xmlHttp.responseText);
								}
							}
		
							xmlHttp.open("GET", "isUsernameTaken.php?newName="+x, true); 
							xmlHttp.send(null);
						}
					}
				
				</script>
			<?php 
			}
			
			function displayConfirmation()
			{
				echo '<div class="spacer"></div>';
				echo '<div class="spacer"></div>';
				echo 'A new user account was successfully created!  Please login in to access your account.';
				echo '<div class="spacer"></div>';
				echo '<div class="spacer"></div>';
			}
			
			function displayError()
			{
				echo 'Something went wrong.  Please try again to create a new account.';
			}
			
			?>
			
		</div>
		<div id="tags">
			<h1>Tags</h1>
			<?php tag::displayTagCloud(); ?>
		</div>
		
		<div id="fblike">
			<?php include "facebookLike.php"; ?>
        </div>
	
	</div>
	
	<div class="clear"></div>
	
	<div id="footer">
		<?php include("footer.php"); ?>
	</div>

</div>


</body>
</html>